package com.yksj.healthtalk.ui.home;

import java.util.ArrayList;
import java.util.List;

import org.universalimageloader.core.ImageLoader;

import android.content.Intent;
import android.graphics.drawable.LevelListDrawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.net.socket.SmartControlClient;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.services.CoreService;
import com.yksj.healthtalk.ui.CommunityActivity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.buyandsell.BuyAndSellMainActivity;
import com.yksj.healthtalk.ui.dictionary.DictionaryMainActivity;
import com.yksj.healthtalk.ui.doctorstation.DocotrStationHelperActivity;
import com.yksj.healthtalk.ui.friend.FriendSearchAboutZxingActivity;
import com.yksj.healthtalk.ui.setting.PersonalInformationActivity;
import com.yksj.healthtalk.ui.setting.SettingQrCodeActivity;
import com.yksj.healthtalk.ui.views.CirclePageIndicator;
import com.yksj.healthtalk.utils.SharePreUtils;
import com.yksj.healthtalk.utils.ViewUtils;
import com.yksj.healthtalk.wallet.MyWalletMainActivity;
/**
 * 我的壹健康  主页
 * @author jack_tang
 *
 */
public class MyHomeMainActivity extends BaseFragmentActivity implements OnClickListener {

	private ViewPager mViewPager;
	private CirclePageIndicator mIndicator;
	private TextView mUser_name;
	private TextView mUser_id;
	private ImageView mUser_icon;
	private ImageLoader imageLoader;
	private ImageView mUser_sex_icon;
	private LevelListDrawable listDrawable;
	private TabsPagerAdapter mAdapter;
	
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.home_main_activity_layout);
		initView();
	}

	private void initView() {
		initTitle();
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("我的壹健康");
		mViewPager = (ViewPager)findViewById(R.id.myviewpager);
		mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
		mAdapter = new TabsPagerAdapter();
		mViewPager.setAdapter(mAdapter);
		mIndicator.setViewPager(mViewPager);
		findViewById(R.id.top_line).setVisibility(View.GONE);//隐藏title上面的线
		
		mUser_icon = (ImageView) findViewById(R.id.user_icon);
		mUser_icon.setOnClickListener(this);
		mUser_id = (TextView) findViewById(R.id.user_id);
		mUser_name = (TextView) findViewById(R.id.user_name);
		imageLoader = ImageLoader.getInstance();
		mUser_sex_icon = (ImageView) findViewById(R.id.user_sex_icon);
		listDrawable = (LevelListDrawable)mUser_sex_icon.getDrawable();
	}

	@Override
	protected void onStart() {
		super.onStart();
		Intent intent =new Intent(this,CoreService.class);
		intent.setAction(CoreService.ACTION_FRAME_VIEW_VISIABLE);
		intent.putExtra("className",this.getClass().getName());
		startService(intent);
	}
	@Override
	protected void onResume() {
		super.onResume();
		CustomerInfoEntity customerInfoEntity = SmartControlClient.getControlClient().getInfoEntity();
		mUser_id.setText(customerInfoEntity.getUsername());
		mUser_name.setText(customerInfoEntity.getName());
		imageLoader.displayImage(customerInfoEntity.getSex(),customerInfoEntity.getNormalHeadIcon(), mUser_icon);
		listDrawable.setLevel(Integer.valueOf(customerInfoEntity.getSex()));
		queryDataCount();
	}
	
	
	//更新未读消息条数界面
	private String meSageCount(int count){
		String readCount;
		if(count!=0){
			readCount=count >100 ? "99+":String.valueOf(count);
		}else{
			readCount="0";
		}
		return readCount;
	}
	
	private void queryDataCount(){
		HttpRestClient.doHttpMyZoneCount("20",SmartFoxClient.getLoginUserId(),SharePreUtils.fatchNewsReadedLastId(),SharePreUtils.fatchInterestWallId(),new JsonsfHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, JSONObject response) {
				super.onSuccess(statusCode, response);//30+40
				try{
					if(response.getInteger("30")==0 && response.getInteger("40")==0){
						ViewUtils.setGone(mAdapter.mFriendNuber,true);
					}else{
						ViewUtils.setGone(mAdapter.mFriendNuber,false);
						mAdapter.mFriendNuber.setText(meSageCount( response.getInteger("30")+response.getInteger("40")));
					}
					
					if(response.getInteger("10")==0 ){
						ViewUtils.setGone(mAdapter.mDoctorNuber,true);
					}else{
						ViewUtils.setGone(mAdapter.mDoctorNuber,false);
						mAdapter.mDoctorNuber.setText(meSageCount( response.getInteger("10")));
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
			
		case R.id.user_icon:
			intent = new Intent(this,PersonalInformationActivity.class);
			this.startActivity(intent);
			break;
			
		case R.id.my_diary://我的档案
			SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),"该场馆暂未开通");
			break;
			
		case R.id.my_doctor://我的医生
			intent = new Intent(this,MyDoctorListActivity.class);
			intent.putExtra("type",0);
			startActivity(intent);
			
			break;
		case R.id.my_friend://我的社区
			intent = new Intent(this, CommunityActivity.class);
			intent.putExtra("id",1);
			startActivity(intent);
			break;
		case R.id.my_salon://我的话题
			intent = new Intent(this,SalonListActivity.class);
			this.startActivity(intent);
			
			break;
		case R.id.medical_knowledge://医学百科
			 intent = new Intent(this,DictionaryMainActivity.class);
			 startActivity(intent);
			
			break;
		case R.id.my_buy://我的购买
			intent =new Intent(this,BuyAndSellMainActivity.class);
			intent.putExtra("type", true);
			this.startActivity(intent);
			
			break;
		case R.id.my_wallet://我的钱包
			intent = new Intent(this,MyWalletMainActivity.class);
			startActivity(intent);
			break;
			
//		case R.id.my_assistant://我的助理
//			intent = new Intent(this,HelperListActivity.class);
//			startActivity(intent);
//			break;
		case R.id.user_guide://使用指南
			intent = new Intent(this,DocotrStationHelperActivity.class);
			intent.putExtra("type","2");
			startActivity(intent);
			break;
			
		case R.id.my_qrcode://我的二维码
			intent = new Intent(this, SettingQrCodeActivity.class);
			intent.putExtra("id", SmartFoxClient.getLoginUserId());
			startActivity(intent);
			break;
			
		case R.id.my_collection://我的商家
			intent = new Intent(this, MyServerCollectActivity.class);
			startActivity(intent);
			break;
		case R.id.seach_qr://扫扫
			intent = new Intent();
			intent.setClass(this, FriendSearchAboutZxingActivity.class);
			intent.putExtra("type","3");// 0-社交场  1-医生馆 2 商户
			startActivity(intent);
			break;
		}
	}
	

	private class TabsPagerAdapter extends PagerAdapter{
		final List<View> views = new ArrayList<View>();
		Button mFriendNuber;
		Button mDoctorNuber;
		public TabsPagerAdapter() {
			View v = getLayoutInflater().inflate(R.layout.home_main_table_fragment_one,null);
			v.findViewById(R.id.my_diary).setOnClickListener(MyHomeMainActivity.this);
			v.findViewById(R.id.my_doctor).setOnClickListener(MyHomeMainActivity.this);
			v.findViewById(R.id.my_friend).setOnClickListener(MyHomeMainActivity.this);
			v.findViewById(R.id.my_buy).setOnClickListener(MyHomeMainActivity.this);
			v.findViewById(R.id.my_wallet).setOnClickListener(MyHomeMainActivity.this);
			v.findViewById(R.id.my_collection).setOnClickListener(MyHomeMainActivity.this);
			v.findViewById(R.id.my_qrcode).setOnClickListener(MyHomeMainActivity.this);
			v.findViewById(R.id.seach_qr).setOnClickListener(MyHomeMainActivity.this);
			mFriendNuber=(Button) v.findViewById(R.id.firend_number_text);
			mDoctorNuber=(Button) v.findViewById(R.id.doctor_number_text);
			views.add(v);
//			v.findViewById(R.id.my_salon).setOnClickListener(MyHomeMainActivity.this);
//			v.findViewById(R.id.medical_knowledge).setOnClickListener(MyHomeMainActivity.this);
//			v.findViewById(R.id.my_buy).setOnClickListener(MyHomeMainActivity.this);
//			v.findViewById(R.id.my_wallet).setOnClickListener(MyHomeMainActivity.this);
//			v.findViewById(R.id.my_assistant).setOnClickListener(MyHomeMainActivity.this);
//			v.findViewById(R.id.user_guide).setOnClickListener(MyHomeMainActivity.this);
			
//			View v2=getLayoutInflater().inflate(R.layout.home_main_table_fragment_two,null);
//			v2.findViewById(R.id.my_collection).setOnClickListener(MyHomeMainActivity.this);
//			v2.findViewById(R.id.my_qrcode).setOnClickListener(MyHomeMainActivity.this);
//			views.add(v2);
		}

		@Override
		public int getItemPosition(Object object) {
			return super.getItemPosition(object);
		}
		
		@Override
		public int getCount() {
			return views.size();
		}
		
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			View item = views.get(position);
			container.addView(item);
			return item;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View)object);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
	}
}
